package com.sky.utils;

import java.net.URI;

import com.sky.constant.MessageConstant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ObjectCannedACL;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;

@Data
@AllArgsConstructor
@Slf4j
public class S3UploadUtil {
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;
    private String region;

    public String upload(byte[] bytes, String objectName) {
        log.info("region: {}", region);
        log.info("endpoint: {}", endpoint);
        log.info("url:{}",URI.create(endpoint));
        // 创建S3Client实例
        S3Client s3Client = S3Client.builder()
                .endpointOverride(URI.create(endpoint)) // 自定义端点
                .credentialsProvider(StaticCredentialsProvider.create(
                        AwsBasicCredentials.create(accessKeyId, accessKeySecret)
                ))
                .region(Region.of(region)) // 区域
                .forcePathStyle(true)      // 强制 Path-style 请求（必须开启）
                .build();

        try {
            // 创建PutObject请求
            s3Client.putObject(
                    PutObjectRequest.builder()
                            .bucket(bucketName)
                            .key(objectName)
                            .acl(ObjectCannedACL.PUBLIC_READ)
                            .build(),
                    RequestBody.fromBytes(bytes)
            );
        } catch (Exception e) {
            log.error("上传文件到S3失败", e);
            throw new RuntimeException(MessageConstant.UPLOAD_FAILED, e);
        }

        // 文件访问路径规则 https://BucketName.Endpoint/ObjectName
        return String.format("%s/%s/%s", endpoint, bucketName, objectName);
    }
}
// public static String uploadFile(S3Client s3Client, String bucket, String publicUrlPrefix, MultipartFile file) throws IOException {
//     String originalFilename = file.getOriginalFilename();
//     if (originalFilename == null) {
//         throw new IllegalArgumentException("文件名不能为空");
//     }
//     String fileExt = originalFilename.substring(originalFilename.lastIndexOf("."));
//     String fileName = UUID.randomUUID() + fileExt;
//
//     s3Client.putObject(
//             PutObjectRequest.builder()
//                     .bucket(bucket)
//                     .key(fileName)
//                     .acl(ObjectCannedACL.PUBLIC_READ)
//                     .build(),
//             RequestBody.fromInputStream(file.getInputStream(), file.getSize())
//     );
//
//     return String.format("%s/%s/%s", publicUrlPrefix, bucket, fileName);
// }
// }